package com.yxkj.syh.app.huarong.adps;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.syh.app.basic.utils.ComUtil;
import com.yxkj.syh.app.huarong.R;
import com.yxkj.syh.app.huarong.bean.Goods;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建者：SYH
 * 时间：2020/5/26 15:26
 * 编辑者：SYH、
 * 标签：
 * 补充说明：
 */
public class AddedGoodsAdp extends RecyclerView.Adapter<AddedGoodsAdp.ViewHolder> {
    List<Goods> data = new ArrayList<>();
    private OnClick mOnClick;

    private boolean isCreator = false;

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_added_goods, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Goods goods = data.get(position);
        holder.tvName.setText(goods.getName());
        holder.tvCount.setText(goods.getPackages() + "包，" + ComUtil.clearNoUseZero(goods.getQuantity()).toPlainString() + "吨");
        if (isCreator && mOnClick != null) {
            holder.itemView.setOnClickListener(v -> {
                mOnClick.onGoodsClick(goods);
            });
        }
        holder.ivMore.setVisibility(isCreator ? View.VISIBLE : View.GONE);
    }

    public void setNewData(List<Goods> goods) {
        data.clear();
        data.addAll(goods);
        notifyDataSetChanged();
    }

    public void add(Goods goods) {
        data.add(goods);
        notifyDataSetChanged();
    }

    public void remove(Goods goods) {
        for (Goods temp : data) {
            if (temp.getCode().equals(goods.getCode())) {
                data.remove(temp);
                break;
            }
        }
        notifyDataSetChanged();
    }

    public void change(Goods goods) {
        for (int i = 0; i < data.size(); i++) {
            Goods temp = data.get(i);
            if (temp.getCode().equals(goods.getCode())) {
                data.set(i, goods);
                break;
            }
        }
        notifyDataSetChanged();
    }

    public void setIsCreator(boolean isCreator) {
        this.isCreator = isCreator;
    }

    public List<Goods> getData() {
        return data;
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public void setOnClick(OnClick onClick) {
        this.mOnClick = onClick;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvName, tvCount;
        ImageView ivMore;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tvName);
            tvCount = itemView.findViewById(R.id.tvCount);
            ivMore = itemView.findViewById(R.id.ivMore);
        }
    }

    public interface OnClick {
        void onGoodsClick(Goods goods);
    }
}
